/*
 * @lc app=leetcode.cn id=14 lang=javascript
 *
 * [14] 最长公共前缀
 */

// @lc code=start
/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function (strs) {
  if (strs == null) return "" // 最长公共前缀
  let length = strs.length
  if (length == 0) return ""
  if (length === 1) {
    return strs[0]
  }
  let mid = Math.floor(length / 2)

  let left = strs.slice(0, mid)
  let right = strs.slice(mid, length)

  return twoCommonPrefix(longestCommonPrefix(left), longestCommonPrefix(right))
}

var twoCommonPrefix = function (str1, str2) {
  let i = 0
  for (; i < str1.length && i < str2.length; i++) {
    if (str1[i] !== str2[i]) break
  }
  return str1.substring(0, i)
}

// @lc code=end
// 与排序链表思路一致，也是分割数组比较，最后递归合并
